package org.dromara.system.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.satoken.utils.LoginHelper;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.PayGroupVo;
import org.dromara.system.domain.bo.PayGroupBo;
import org.dromara.system.service.IPayGroupService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 轮询组
 * 前端访问路由地址为:/system/PayGroup
 *
 * @author Gavino
 * @date 2024-10-31
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/PayGroup")
public class PayGroupController extends BaseController {

    private final IPayGroupService payGroupService;

    /**
     * 复制轮询组
     *
     * @param bo 参数
     * @return 结果
     */
    @SaCheckPermission("system:PayGroup:add")
    @RepeatSubmit()
    @PostMapping("/copy")
    public R<?> copy(@RequestBody PayGroupBo bo) {
//        if (!LoginHelper.isAgent()) {
//            return R.fail("只有代理才可复制轮询组");
//        }
        if (bo.getGroupId() == null) {
            return R.fail("轮询组ID不能为空");
        }
        if (StringUtils.isEmpty(bo.getGroupName())) {
            return R.fail("轮询组名称不能为空");
        }

        return payGroupService.copy(bo.getGroupId(), bo.getGroupName());
    }

    /**
     * 查询轮询组列表
     */
    @GetMapping("/list")
    public TableDataInfo<PayGroupVo> list(PayGroupBo bo, PageQuery pageQuery) {
        return payGroupService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出轮询组列表
     */
    @SaCheckPermission("system:PayGroup:export")
    @Log(title = "轮询组", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PayGroupBo bo, HttpServletResponse response) {
        List<PayGroupVo> list = payGroupService.queryList(bo);
        ExcelUtil.exportExcel(list, "轮询组", PayGroupVo.class, response);
    }

    /**
     * 获取轮询组详细信息
     *
     * @param groupId 主键
     */
    @SaCheckPermission("system:PayGroup:query")
    @GetMapping("/{groupId}")
    public R<PayGroupVo> getInfo(@NotNull(message = "主键不能为空")
                                 @PathVariable Long groupId) {
        return R.ok(payGroupService.queryById(groupId));
    }

    /**
     * 新增轮询组
     */
    @SaCheckPermission("system:PayGroup:add")
    @Log(title = "轮询组", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PayGroupBo bo) {
        if (!LoginHelper.isAgent()) {
            return R.fail("只有代理才可添加轮询组");
        }
        return toAjax(payGroupService.insertByBo(bo));
    }

    /**
     * 修改轮询组
     */
    @SaCheckPermission("system:PayGroup:edit")
    @Log(title = "轮询组", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PayGroupBo bo) {
        return toAjax(payGroupService.updateByBo(bo));
    }

    /**
     * 删除轮询组
     *
     * @param groupIds 主键串
     */
    @SaCheckPermission("system:PayGroup:remove")
    @Log(title = "轮询组", businessType = BusinessType.DELETE)
    @DeleteMapping("/{groupIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] groupIds) {
        return toAjax(payGroupService.deleteWithValidByIds(List.of(groupIds), true));
    }
}
